package com.zhangzeqin.code.view;

import com.zhangzeqin.code.controller.TimeController;
import com.zhangzeqin.code.controller.WordBookController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class LearnWordView extends JFrame {

    public static void main(String[] args) {
        new LearnWordView();
    }
    String word;
    JLabel nameLabel=new JLabel("单词界面",JLabel.CENTER);
    //弹性布局
    SpringLayout springLayout=new SpringLayout();
    JPanel centerPanel=new JPanel(springLayout);
    JLabel wordLabel=new JLabel();
    JLabel meaningLabel=new JLabel();
    JButton returnButton=new JButton("返回");
    JButton knowButton=new JButton("认识");
    JButton unknowButton=new JButton("不认识");
    JButton nextButton=new JButton("下一个");
    SystemTray systemTray;
    TrayIcon trayIcon;

    /**
     * 时间控制层
     */
    TimeController timeController=new TimeController();
    /**
     * 两个时间戳
     */
    long startTime=0;
    long endTime=0;

    WordBookController wordBookController = new WordBookController();
    java.util.List<Map> list;
    Map map;
    Set keySet;
    Iterator iterator;
    Object key;
    Object value;
    int index =0;

    public LearnWordView() {
        super("单词学习");
        //获取内容面板
        Container contentPane = getContentPane();
        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 80));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        wordLabel.setFont(centerFont);
        meaningLabel.setFont(centerFont);
        knowButton.setFont(centerFont);
        unknowButton.setFont(centerFont);
        returnButton.setFont(centerFont);
        nextButton.setFont(centerFont);

        //把组件加入面板
        centerPanel.add(wordLabel);
        centerPanel.add(meaningLabel);

        //增加按钮事件
        //centerPanel.add(checkoutButton);
        centerPanel.add(knowButton);
        centerPanel.add(unknowButton);
        centerPanel.add(returnButton);
        centerPanel.add(nextButton);
        //checkoutButton.addActionListener();

        //弹簧布局
        layout(contentPane);

        //增加系统托盘
        addTray();


        //自定义图标
        URL imgUrl = LoginView.class.getClassLoader().getResource("image.png");
        setIconImage(new ImageIcon(imgUrl).getImage());

        //窗口大小
        setSize(600, 400);
        //计算位置居中
        setLocationRelativeTo(null);
        //关闭退出程序
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        //可视
        setVisible(true);

        try {
            list = wordBookController.getWord("word_book1");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        displayWord();

        //"认识"按钮监听器
        knowButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    startTime = System.currentTimeMillis();
                    System.out.println(startTime);
                    index++;
                    displayWord();
                    wordLabel.setText((String) key);
                    meaningLabel.setText(null);
                }
            });

        //"不认识"按钮监听器
        unknowButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    meaningLabel.setText((String) value);
                    displayWord();

                }
            });

        //"下一个"按钮监听器
        nextButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    index++;
                    displayWord();
                    wordLabel.setText((String) key);
                    meaningLabel.setText(null);
                }
            });
        //"返回"按钮监听器
            returnButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    endTime = System.currentTimeMillis();
                    System.out.println(endTime);
                    new WordBookView();
                    timeController.updateTime(startTime,endTime);

                }
            });

        }

        //单词集合解析方法
    private void displayWord() {
        map = list.get(index);
        keySet = map.keySet();
        iterator = keySet.iterator();
        key = iterator.next();
        value = map.get(key);
        wordLabel.setText((String) key);
    }


    public JLabel getNameLabel() {
        return nameLabel;
    }

    public void setNameLabel(JLabel nameLabel) {
        this.nameLabel = nameLabel;
    }

    public SpringLayout getSpringLayout() {
        return springLayout;
    }

    public void setSpringLayout(SpringLayout springLayout) {
        this.springLayout = springLayout;
    }

    public JPanel getCenterPanel() {
        return centerPanel;
    }

    public void setCenterPanel(JPanel centerPanel) {
        this.centerPanel = centerPanel;
    }

    public JLabel getWordLabel() {
        return wordLabel;
    }

    public void setWordLabel(JLabel wordLabel) {
        this.wordLabel = wordLabel;
    }


    public JButton getReturnButton() {
        return returnButton;
    }

    public void setReturnButton(JButton returnButton) {
        this.returnButton = returnButton;
    }

    public JLabel getMeaningLabel() {
        return meaningLabel;
    }

    public void setMeaningLabel(JLabel meaningLabel) {
        this.meaningLabel = meaningLabel;
    }



    public JButton getKnowButton() {
        return knowButton;
    }

    public void setKnowButton(JButton knowButton) {
        this.knowButton = knowButton;
    }

    public JButton getUnknowButton() {
        return unknowButton;
    }

    public void setUnknowButton(JButton unknowButton) {
        this.unknowButton = unknowButton;
    }

    public SystemTray getSystemTray() {
        return systemTray;
    }

    public void setSystemTray(SystemTray systemTray) {
        this.systemTray = systemTray;
    }

    public TrayIcon getTrayIcon() {
        return trayIcon;
    }

    public void setTrayIcon(TrayIcon trayIcon) {
        this.trayIcon = trayIcon;
    }

    /**
     * 弹簧布局
     * @param contentPane
     */
    private void layout(Container contentPane) {
        //布局wordLabel
        Spring childWidth=Spring.sum(Spring.width(wordLabel), Spring.constant(20));
        int offSetX=childWidth.getValue()/2;
        springLayout.putConstraint(SpringLayout.WEST,wordLabel,-offSetX,
                SpringLayout.HORIZONTAL_CENTER,centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH,wordLabel,20,SpringLayout.NORTH,centerPanel);
        //meaningLabel
        springLayout.putConstraint(SpringLayout.EAST,meaningLabel,20,SpringLayout.EAST,wordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,meaningLabel,40,SpringLayout.SOUTH,wordLabel);
       //knowButton
        springLayout.putConstraint(SpringLayout.WEST,knowButton,-120,SpringLayout.WEST,wordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,knowButton,50,SpringLayout.NORTH,meaningLabel);
        //unknowButton
        springLayout.putConstraint(SpringLayout.WEST,unknowButton,30,SpringLayout.EAST,knowButton);
        springLayout.putConstraint(SpringLayout.NORTH,unknowButton,0,SpringLayout.NORTH,knowButton);
        //returnButton
        springLayout.putConstraint(SpringLayout.WEST,returnButton,50,SpringLayout.EAST,unknowButton);
        springLayout.putConstraint(SpringLayout.NORTH,returnButton,80,SpringLayout.NORTH,unknowButton);
        //nextButton
       springLayout.putConstraint(SpringLayout.WEST,nextButton,30,SpringLayout.EAST,unknowButton);
        springLayout.putConstraint(SpringLayout.NORTH,nextButton,0,SpringLayout.NORTH,unknowButton);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerPanel,BorderLayout.CENTER);
    }

    /**
     * 增加系统托盘
     */
    private void addTray() {
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();
            URL imgUrl=LoginView.class.getClassLoader().getResource("tray.png");
            trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());
            //设置托盘图片大小自动缩放
            trayIcon.setImageAutoSize(true);
            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
            //增加最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {

                @Override
                public void windowIconified(WindowEvent e) {
                    LearnWordView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount = e.getClickCount();
                    if (clickCount == 1) {
                       LearnWordView.this.setExtendedState(JFrame.NORMAL);
                    }
                    LearnWordView.this.setVisible(true);
                }
            });
        }
    }
}
